package
{
	import as3isolib.core.IIsoDisplayObject;
	import as3isolib.core.IsoDisplayObject;
	import as3isolib.display.primitive.IsoBox;
	import as3isolib.display.scene.IsoScene;
	import as3isolib.geom.Pt;
	
	import flash.display.DisplayObject;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	import flash.utils.getDefinitionByName;

	public class GameObject implements ISimObject, ISerializable
	{
		public var model:GameObjectModel;
		public var isoDisplay:IIsoDisplayObject;
		
		public function GameObject(model:GameObjectModel):void{
			this.model = model;
		}
		
		public function initializeRendering():void{
			this.isoDisplay = createIsoDisplay();
			this.isoDisplay.setSize(this.model.size.x, this.model.size.y, this.model.size.z);
			this.isoDisplay.x = this.model.location.x;
			this.isoDisplay.y = this.model.location.y;
			this.isoDisplay.z = this.model.location.z;			
		}
		
		public function get className():String{return '';}
		
		private function createIsoDisplay():IIsoDisplayObject{
			return new IsoBox();
		}
		
		public function updateCurrentState():void{
			Global.setLocation(this.isoDisplay, model.location);
		}
		
		public function setupNextState(delta:int):void{
			
		}	
		
		public function hitTest(point:Point):Boolean{
			return isoDisplay.container.hitTestPoint(point.x, point.y);	
		}
		
		
		public function serialize():Object{
			var serData:Object = new Object();
			serData.className = this.className;
			serData.x = model.location.x;
			serData.y = model.location.y;
			serData.z = model.location.z;
			serData.sizeX = model.size.x;
			serData.sizeY = model.size.y;
			serData.sizeZ = model.size.z;
			return serData;
		}
		
		public function onBuyFinished():void{
			
		}
		
		public function wakeup(serData:Object):void{
			model.location = new Vector3D(serData.x, serData.y, serData.z);
			model.size = new Vector3D(serData.sizeX, serData.sizeY, serData.sizeZ);
		}
				
		
		public static function serializeVector(vector:Vector.<GameObject>):Array{
			var i:int, length:int = vector.length;
			var serialized:Array = new Array();
			for(i = 0; i < length; i++){
				serialized.push(vector[i].serialize());
			}
			return serialized;
		}
		
		public static function unserializeVector(serDatas:Array, gameObjects:Vector.<GameObject>):void{
			var i:int, length:int = serDatas.length;
			for(i = 0; i < length; i++){
				var classRef:Class = getDefinitionByName(serDatas[i].className) as Class;
				var gameObject:GameObject = new classRef(new GameObjectModel());
				gameObject.wakeup(serDatas[i]);
				gameObject.initializeRendering();
				gameObjects.push(gameObject);
			}
		}		
				
	}
}